package com.cqc.walelock01;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private Button btn2;
    private Intent monitorService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //-----------------------------------------------
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(new Intent(MainActivity.this, WakeLockService.class));
                if (!MonitorService.isRunning) {
                    // 开启监听service
                    MonitorService.isCheck = true;
                    MonitorService.isRunning = true;
                    monitorService = new Intent(MainActivity.this, MonitorService.class);
                    startService(monitorService);
                }
            }
        });
        btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 停止监听service
                MonitorService.isCheck = false;
                MonitorService.isRunning = false;
                if (null != monitorService) {
                    stopService(monitorService);
                    Log.d("MonitorService2", "null != monitorService");
                }
                //不能立即停止，需要过一段时间
//                stopService(new Intent(MainActivity.this, WakeLockService.class));
                // 通过轨迹服务客户端client停止轨迹服务
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stopService(new Intent(MainActivity.this, WakeLockService.class));
                    }
                }, 10 * 1000);

            }
        });

    }

}
